package com.example.chenjun.myapplication.net.okhttp.request;

import android.support.annotation.NonNull;

import java.io.File;
import java.util.Map;
import java.util.Set;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MultiRequest extends OkHttpRequest {
    private File mFile;
    private Map<String, File> mFileMap;
    public MultiRequest(Map<String, String> params, Map<String, String> headers, @NonNull String url, String tag, File file, Map<String, File> files) {
        super(params, headers, url, tag, "POST");
        mFile = file;
        mFileMap = files;
    }

    @Override
    protected RequestBody buildRequestBody() {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        if (params != null){
            Set<String> keySet = params.keySet();
            for (String key : keySet) {
                builder.addFormDataPart(key, params.get(key));
            }
        }
        if (mFile != null){
            builder.addPart(RequestBody.create(null, mFile));
        }
        if (mFileMap != null){
            Set<String> keySet = mFileMap.keySet();
            for (String key : keySet) {
                File file = mFileMap.get(key);
                builder.addFormDataPart(key, file.getName(), RequestBody.create(null, mFile));
            }
        }
        return builder.build();
    }
}
